package com.example.administrator.social.util.request;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.HttpHeaderParser;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/15 0015.
 */
public class CustomRequest extends Request<String> {
    /** http请求编码方式 */
    private static final String PROTOCOL_CHARSET = "utf-8";
    private Response.Listener<String> mListener;
    private Map<String,String> mMap;
    public CustomRequest(String url, int type, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(type, url, errorListener);
        mListener = listener;
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }

    @Override
    protected void deliverResponse(String response) {
        mListener.onResponse(response);
    }

    public void setParamsMap(Map<String,String> map){
        mMap = map;
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        if(mMap!=null){
            return mMap;
        }
        return super.getParams();
    }
}
